//
//  ZYProblemRecoverOneVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/30.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYProblemRecoverOneVC.h"
#import "ZYProblemRecoverTwoVC.h"
#import "ZYRecoverPasswordViewModel.h"

@interface ZYProblemRecoverOneVC ()
@property(nonatomic,strong) UITextField *usernameTextField;
@property(nonatomic,strong) UIButton *confirmBtn;

/**
 校验用户名
 */
@property(nonatomic,strong) ZYRecoverPasswordViewModel *viewModel;
@end

@implementation ZYProblemRecoverOneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self setupTap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVER_CONTROLLER_TITLE];
}

-(void)setupView{
    CGFloat left = 20;
    CGFloat height = 40;
    UITextField *usernameTextField = [[UITextField alloc] init];
    usernameTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:usernameTextField];
    usernameTextField.centerX = self.view.centerX;
    usernameTextField.top = self.navigationView.bottom + 20;
    [self setupTextField:usernameTextField];
    [self setLeftView:usernameTextField];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.bounds = usernameTextField.bounds;
    [self.view addSubview:confirmBtn];
    confirmBtn.centerX = self.view.centerX;
    confirmBtn.top = usernameTextField.bottom + 65;
    [ZYUtils setGradualChangingColorWithButton:confirmBtn size:confirmBtn.size];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _confirmBtn = confirmBtn;
    _usernameTextField = usernameTextField;
    [self setupTitle];
}

-(void)setupTextField:(UITextField *)textFiled{
    textFiled.layer.borderWidth = 1;
    textFiled.layer.borderColor = RGBA(235, 235, 235, 1).CGColor;
    textFiled.layer.cornerRadius = 5;
    textFiled.layer.masksToBounds = YES;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
}

/**
 初始化标题
 */
-(void)setupTitle{
    NSString *usernamePlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVER_USERNAME_PLACEHOLDER];
    NSString *confirmBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE];
    [_confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
    _usernameTextField.placeholder = usernamePlaceholder;
}

/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

#pragma mark -Event
//确认
-(void)doConfirm:(id)button{
    [self dismissKeyboard];
    NSString *username = _usernameTextField.text;
    self.viewModel.phone = username;
    //监听状态
    [[self.viewModel.usernameCheckCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        ((UIButton *)button).enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
    @weakify(self);
    [[self.viewModel.usernameCheckCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        ZYProblemRecoverTwoVC *vc = [[ZYProblemRecoverTwoVC alloc] init];
        vc.username = username;
        [self.navigationController pushViewController:vc animated:YES];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    
    
}

#pragma mark - Getter
-(ZYRecoverPasswordViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYRecoverPasswordViewModel alloc] init];
    }
    return _viewModel;
}

@end
